package com.example.light.web.personal.controller;

import com.example.light.common.result.CodeMsg;
import com.example.light.common.result.Result;
import com.example.light.web.personal.entity.NameCard;
import com.example.light.web.personal.service.NameCardService;
import com.example.light.web.system.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("namecard")
public class NameCardController {

    @Autowired
    private NameCardService nameCardService;

    @RequestMapping("toNameCardPage")
    public String toNameCardPage(User user, Model model){
        nameCardService.showNamecards(user.getUserId(),model);
        return "personal/contacts";
    }

    @RequestMapping("toNameCardAdd")
    public String toNameCardAdd(){
        return "personal/contacts_add";
    }

    @RequestMapping("addNameCard")
    @ResponseBody
    public Result<Boolean> addNameCard(NameCard nameCard, User user){
        boolean res = nameCardService.insert(nameCard,user.getUserId());
        if(res){
            return Result.success(true);
        }
        return Result.error(CodeMsg.PersonalCodeMsg.NAME_CARD_ADD_FAIL);
    }

    @RequestMapping("modifyNameCard")
    @ResponseBody
    public Result<Boolean> modifyNameCard(NameCard nameCard, User user){
        boolean res = nameCardService.update(nameCard,user.getUserId());
        if(res){
            return Result.success(true);
        }
        return Result.error(CodeMsg.PersonalCodeMsg.NAME_CARD_MODIFY_FAIL);
    }

    @RequestMapping("delNameCard")
    @ResponseBody
    public Result<Boolean> delNameCard(NameCard nameCard, User user){
        boolean res = nameCardService.del(nameCard.getNameCardId());
        if(res){
            return Result.success(true);
        }
        return Result.error(CodeMsg.PersonalCodeMsg.NAME_CARD_MODIFY_FAIL);
    }

    @RequestMapping("toNameCardModify/{id}")
    public String toNameCardModify(@PathVariable("id")Integer id,Model model){
        nameCardService.selectOne(id,model);
        return "personal/contacts_modify";
    }
}
